package com.rockwell.sniffyhunter.model;

import java.util.ArrayList;

import android.content.Context;

import com.rockwell.sniffyhunter.R;
import com.rockwell.sniffyhunter.SettingsActivity;

public class MazePath {
	public enum Direction {
		Left,
		Right,
		Up,
		Down
	}
	
	public enum MazeNo {
		Entrance,
		Easy,
		Medium,
		Hard
	}
	
	public final static String[] junctionTypes = new String[] { 
		"None",
		"Left-turn only", 
		"Right-turn only", 
		"T junction",
		"T with left-turn", 
		"T with right-turn", 
		"Cross junction",
		"Dead end" 
		};

	public final static Integer[] junctionIcons = new Integer[] { 
		R.drawable.empty,
		R.drawable.j_lo, 
		R.drawable.j_ro, 
		R.drawable.j_t, 
		R.drawable.j_tl,
		R.drawable.j_tr, 
		R.drawable.j_x, 
		R.drawable.j_d 
		};

	public final static int JUNCTION_NONE = 0;
	public final static int JUNCTION_LO = 1;
	public final static int JUNCTION_RO = 2;
	public final static int JUNCTION_T = 3;
	public final static int JUNCTION_TL = 4;
	public final static int JUNCTION_TR = 5;
	public final static int JUNCTION_X = 6;
	public final static int JUNCTION_D = 7;
	
	public final static String[] directionTypes = new String[] { 
			"None", 
			"Go straight",
			"Turn left", 
			"Turn right", 
			"U-turn", 
			};

	public final static Integer[] directionIcons = new Integer[] { 
			R.drawable.empty,
			R.drawable.a_f, 
			R.drawable.a_l, 
			R.drawable.a_r, 
			R.drawable.a_u 
			};

	public final static int DIRECTION_NONE = 0;
	public final static int DIRECTION_FORWARD = 1;
	public final static int DIRECTION_LEFT = 2;
	public final static int DIRECTION_RIGHT = 3;
	public final static int DIRECTION_UTURN = 4;
	
	
	public static boolean loadPredefinedPath(Context context,
			ArrayList<RobotAction> directions) {

		String path = loadPredefinedPath(context);
		return parsePathString(path, directions);
	}
	
	public static String loadPredefinedPath(Context context) {
		String path = SettingsActivity.getPredefinedPath(context);
		return path;
	}

	public static boolean parsePathString(String path,
			ArrayList<RobotAction> directions) {
		if (path == "") {
			return false;
		}

		directions.clear();

		for (int i = 0; i < path.length(); i++) {
			int dTag = Integer.valueOf(String.valueOf(path.charAt(i)));

			switch (dTag) {
			case DIRECTION_FORWARD:
				directions.add(RobotAction.Jog);
				break;

			case DIRECTION_LEFT:
				directions.add(RobotAction.TurnLeft);
				break;

			case DIRECTION_RIGHT:
				directions.add(RobotAction.TurnRight);
				break;

			case DIRECTION_UTURN:
				directions.add(RobotAction.TurnBack);
				break;
			}
		}

		return true;
	}

	public static boolean isValidDirection(int junction, int direction) {
		switch (junction) {
		case JUNCTION_NONE:
			return true;
			
		case JUNCTION_LO:
			return direction == DIRECTION_LEFT;
			
		case JUNCTION_RO:
			return direction == DIRECTION_RIGHT;
			
		case JUNCTION_T:
			return direction != DIRECTION_FORWARD;
			
		case JUNCTION_TL:
			return direction != DIRECTION_RIGHT;
			
		case JUNCTION_TR:
			return direction != DIRECTION_LEFT;
			
		case JUNCTION_X:
			return true;
			
		case JUNCTION_D:
			return direction == DIRECTION_UTURN;
			
			default:
				return false;
		}
	}
	

}
